package com.ay.selfservicemachine.http.download;



import java.io.File;
import java.util.HashMap;

/**
 * 下载中枢
 */
public class DownloadComm {
    static HashMap<String, DownloadExecute> downloadListenerHashMap = new HashMap<>();


    public static void DownloadListenerBind(String url, String filePath, DownloadListener downloadListener) {//下载监听事件绑定
        if (downloadListenerHashMap.containsKey(filePath)) {
            downloadListenerHashMap.get(filePath).downloadListener = downloadListener;
        } else {
            DownloadExecute downloadExecute = new DownloadExecute();
            downloadExecute.startDownloadFile(url, new File(filePath), new DownloadListener() {
                @Override
                public void onDownloading(File file, long progress, long max, String HQWprogress, String HQWmax) {
                    if (downloadListener!=null){
                        downloadListener.onDownloading(file, progress, max, HQWprogress, HQWmax);
                    }

                }

                @Override
                public void onSucceed(File file) {
                    if (downloadListener!=null){
                        downloadListener.onSucceed(file);
                        downloadListenerHashMap.remove(filePath);
                    }

                }

                @Override
                public void onStop(File file) {
                    if (downloadListener!=null){
                        downloadListener.onStop(file);
                        downloadListenerHashMap.remove(filePath);
                    }

                }

                @Override
                public void onFailed(File file, Exception e) {
                    downloadListener.onFailed(file, e);
                    downloadListenerHashMap.remove(filePath);
                }
            });
            downloadListenerHashMap.put(filePath, downloadExecute);
        }
    }

    public static void DownloadListenerUnBind(String filePath) {//下载监听事件移除绑定
        if (downloadListenerHashMap.containsKey(filePath)) {
            downloadListenerHashMap.get(filePath).downloadListener = null;
            downloadListenerHashMap.remove(filePath);
        }
    }


}
